package com.lifesaftypower.activity;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lifesaftypower.adapter.ProductModelAdapter;
import com.lifesaftypower.product.ProductSeries;
import com.lifesaftypower.util.CacheManager;
import com.lifesaftypower.util.HttpUtil;

public class ProductActivity extends Activity {
	private class LoadResourceTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			String imgUrl = _series.get_img_url();
			if (imgUrl != null && imgUrl.length() > 0) {
				InputStream ins = HttpUtil.downloadUrl(imgUrl);
				if (CacheManager.HasUrlResourceInCache(imgUrl)) {
					CacheManager.PutUrlResourceIntoCache(imgUrl, ins);
				}
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void param) {
			ImageView jpgView = (ImageView) findViewById(R.id.imgProduct);
			String imgUrl = _series.get_img_url();
			InputStream imgStream = CacheManager.GetUrlResourceFromCache(imgUrl);
			Bitmap bitmap = BitmapFactory.decodeStream(imgStream);
			jpgView.setImageBitmap(bitmap);
			try {
				imgStream.close();
			} catch (IOException e) {			
				e.printStackTrace();
			}
			
			TextView vName = (TextView)findViewById(R.id.tvSeriesName);			
			vName.setText(_series.get_description());
			vName.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(ProductActivity.this, OverviewActivity.class);
					intent.putExtra(OverviewActivity.INTENT_OVERVIEW, _series.get_overview());
					startActivity(intent);					
				}
				
			});
			
			WebView vOverview = (WebView)findViewById(R.id.wvModelOverview);
			vOverview.loadData(_series.get_model_overview(), "text/html", null);
			
		}

		@Override
		protected void onPreExecute() {

		}
	}

	public static final String INTENT_PRODUCT = "INTENT_PRODUCT";

	private ProductSeries _series;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_product);

		Bundle extra = getIntent().getExtras();

		_series = (ProductSeries) (extra.getSerializable(INTENT_PRODUCT));

		LoadResourceTask task = new LoadResourceTask();
		task.execute(new Void[] {});

		ListView modelListView = (ListView) findViewById(R.id.modelListView);
		ProductModelAdapter modelAdapter = new ProductModelAdapter(this,
				R.layout.product_list_item, _series.get_products());

		modelListView.setAdapter(modelAdapter);
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_product, menu);
		return true;
	}

}
